{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "47d21239-bc11-47bb-9bb7-953d16df5035",
   "metadata": {},
   "source": [
    "Chapter 08\n",
    "# 利用矩阵转置判断矩阵是否对称\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "05380956-847b-4d78-8d5a-8f8094e89f25",
   "metadata": {},
   "outputs": [],
   "source": [
    "def is_symmetric_2(matrix):\n",
    "    rows = len(matrix)\n",
    "    cols = len(matrix[0])\n",
    "    # 首先判断矩阵是否为方阵\n",
    "    if rows != cols:\n",
    "        return False \n",
    "    # 获得转置矩阵\n",
    "    tranposed = [[(matrix[j][i]) \n",
    "                  for j in range(rows)] \n",
    "                 for i in range(rows)]\n",
    "    if(matrix == tranposed):\n",
    "        return True\n",
    "    return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0282b284-0d8a-4ac6-abdd-b060f85bf40f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 两个矩阵\n",
    "A = [[1, 2, 3],\n",
    "     [2, 4, 5],\n",
    "     [3, 5, 6]]\n",
    "\n",
    "B = [[1, 2, 3],\n",
    "     [2, 4, 0],\n",
    "     [0, 5, 6]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bdef5438-541c-40fb-be2f-774921e0ed9a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "是否为对称矩阵: True\n"
     ]
    }
   ],
   "source": [
    "print(\"是否为对称矩阵:\", is_symmetric_2(A))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "92e658b0-9c5b-43ab-9eae-98f84861cb6f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "是否为对称矩阵: False\n"
     ]
    }
   ],
   "source": [
    "print(\"是否为对称矩阵:\", is_symmetric_2(B))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
